/*   **********************************************************************  **
 **   Copyright notice                                                       **
 **                                                                          **
 **   (c) 2009, Around(J2)ME				                                 **
 **   All rights reserved.                                                   **
 **                                                                          **
 **   This program and the accompanying materials are made available under   **
 **   the terms of the New BSD License which accompanies this    			 **
 **   distribution.											                 **
 **   A copy is found in the textfile LICENSE.txt							 **
 **                                                                          **
 **   This copyright notice MUST APPEAR in all copies of the file!           **
 **                                                                          **
 **   Main developers:                                                       **
 **     Juri Strumpflohner		http://blog.js-development.com	             **
 **		Matthias Braunhofer		http://matthias.jimdo.com                    **
 **                                                                          **
 **  **********************************************************************  */

package com.aroundj2me.view;

import java.util.Vector;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;

import com.aroundj2me.controller.ApplicationController;
import com.aroundj2me.controller.ScreenController;
import com.aroundj2me.model.Place;
import com.aroundj2me.utility.ImageLoader;
/**
 * View for displaying a list of places
 *
 */
public class PlaceListView implements CommandListener {
	private ApplicationController controller;
	private List placeList;
	//TODO could pose memory problems if many places...think of retrieving partially (<20, between 20 and 40,...)
	private Vector places; //holds all the place-objects
	private final int CHARSBREAK = 30;
	
	//commands
	private Command deletePlace;
	private Command editPlace;
	
	public PlaceListView(String title, ApplicationController parent) {
		this.controller = parent;
		this.placeList = new List("Places", List.IMPLICIT);
		this.placeList.setCommandListener(this);
		createCommands();
	}

	/**
	 * Creates the commands
	 *
	 */
	private void createCommands() {
		deletePlace = new Command("Delete", "Delete place", Command.SCREEN,0);
		editPlace = new Command("Edit", "Edit place", Command.SCREEN, 0);
		placeList.addCommand(deletePlace);
		placeList.addCommand(editPlace);
	}
	
	/**
	 * loads a list of places on the UI
	 *
	 */
	public void loadList(){
		if(places != null){
			for(int i=0; i<places.size(); i++){
				Place tmp = (Place)places.elementAt(i);
				String name = tmp.getName();
				if(name.length()>CHARSBREAK)
					name = name.substring(0,CHARSBREAK) + "...";
				
				placeList.append(name, ImageLoader.getImage(ImageLoader.POST));
			}
			if(places.size()>=controller.getMAX_DATA_AMOUNT())
				placeList.append("Fetch next places", ImageLoader.getImage(ImageLoader.NEXT));
		}
	}
	
	/**
	 * Returns the selected place of the list
	 * @return selected {@link Place} object
	 */
	public Place getSelectedPlace(){
		return (Place) places.elementAt(placeList.getSelectedIndex());
	}

	/**
	 * Action handler
	 */
	public void commandAction(Command c, Displayable d) {
		if(c.getCommandType() == Command.BACK){
			ScreenController.getInstance().last();
		}else{
			String itemText = placeList.getString(placeList.getSelectedIndex());
			boolean isNextCommand = false;
			try{
				isNextCommand = itemText.substring(0, 10).equals("Fetch next");
			}catch (Exception e){
				isNextCommand = false;
			}
			if(isNextCommand){
				controller.fetchNextPlaces();
			}else{
				if(c == deletePlace){
					controller.deletePlace();
				}else if(c == editPlace){
					controller.editPlace(getSelectedPlace());
				}else if(c.getCommandType()==1 && d == placeList){
					int index = placeList.getSelectedIndex();
					Place p = (Place) places.elementAt(index);
					controller.displayPlace(p);
				}
			}
		}
	}
	
	/**
	 * Removes the selected Place-entry from the list
	 *
	 */
	public void removeSelectedEntry() {
		int index = placeList.getSelectedIndex();
		placeList.delete(index);
		places.removeElementAt(index);
	}
	
	/**
	 * Resets the object in order to reuse them without having the
	 * need to instantiate a new one
	 *
	 */
	public void reset(){
		placeList.deleteAll();
	}
	
	/*
	 * Getters and Setters
	 */
	
	public List getPlaceList() {
		return placeList;
	}

	public Vector getPlaces() {
		return places;
	}

	public void setPlaces(Vector places) {
		this.places = places;
	}

}
